package com.my.swas.photo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.Arrays;
import java.util.Properties;

/**
 * @Description:
 * @author: :MaYong
 */
@Configuration
public class BeanConfiguration {
    @Autowired
    Environment environment;

    @Bean
    public YamlConfigUtil ymlConfigurerUtil() {

        String[] strings = environment.getActiveProfiles();
        //1:加载配置文件
        Resource app = null;
        if(strings.length == 0){
            app = new ClassPathResource("application.yml");
        }else if(Arrays.asList(strings).contains("dev")){
            app = new ClassPathResource("application-dev.yml");
        }else if(Arrays.asList(strings).contains("prod")){
            app = new ClassPathResource("application-prod.yml");
        }else if(Arrays.asList(strings).contains("test")){
            app = new ClassPathResource("application-test.yml");
        }
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        // 2:将加载的配置文件交给 YamlPropertiesFactoryBean
        yamlPropertiesFactoryBean.setResources(app);
        // 3：将yml转换成 key：val
        Properties properties = yamlPropertiesFactoryBean.getObject();
        // 4: 将Properties 通过构造方法交给我们写的工具类
        YamlConfigUtil yamlConfigUtil = new YamlConfigUtil(properties);
        return yamlConfigUtil;
    }
}
